package com.cn.qgx428.lazydiary.ui.timeline

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.Utils.RecordHelper
import com.cn.qgx428.lazydiary.adapter.RAdapter
import com.cn.qgx428.lazydiary.adapter.RViewHolder
import com.cn.qgx428.lazydiary.db.bean.CBill
import com.cn.qgx428.lazydiary.db.dao.BillDao
import com.cn.qgx428.lazydiary.extension.getCurrenMouthWidthYear
import com.cn.qgx428.lazydiary.ui.activity.BillActivity
import kotlinx.android.synthetic.main.timeline_bill.view.*
import org.jetbrains.anko.startActivity
import java.util.*

/**
 * Created by qinguanxiong on 2018/4/5.
 */
class T_Bill(context: Context, mView: View?) : T_Base(context, mView) {

    lateinit var recycler: RecyclerView
    lateinit var mainLayout: LinearLayout
    lateinit var adapter : BillAdapter

    override fun initView() {
        recycler = getView(R.id.timeline_bill_re)
        mainLayout = getView(R.id.timeline_bill_layout)
        adapter = BillAdapter(mContext, R.layout.adapter_timeline_bill_re)
        recycler.adapter = adapter
        recycler.layoutManager = LinearLayoutManager(mContext)
    }

    override fun initEvent() {

    }

    override fun getData() {
        val dao = BillDao()
        val bills = dao.getBuilder()
                .orderBy("createTime", false)
                .limit(3)
                .query()
        if(bills!=null && bills.size>0){
            adapter.setDatas(bills)
            mainLayout.visibility = View.VISIBLE
        } else{
            mainLayout.visibility = View.GONE
        }
    }

    override fun onDataChange() {
        getData()
    }

    class BillAdapter : RAdapter<CBill>{
        constructor(context: Context, layoutId: Int):
                super(context, layoutId)

        override fun conver(holder: RViewHolder, data: CBill) {
            val total_txt  = holder.getView<TextView>(R.id.adapter_timeline_bill_total)
            val specific_txt = holder.getView<TextView>(R.id.adapter_timeline_bill_specific)
            val icon = holder.getView<ImageView>(R.id.adapter_timeline_bill_typeIcon)
            val mainLayout = holder.getView<LinearLayout>(R.id.adapter_timeline_bill_layout);

            specific_txt?.text = data.specific
            icon?.setImageResource(RecordHelper.getBillIcon(data.type!!, data.specific!!))
            // 根据收入支出类型改变颜色
            if (data.type.equals(MConst.BILL_INCOME)){
                total_txt!!.text = "+" + data.total
                total_txt.setTextColor(MConst.incomeTextColor)
            } else{
                total_txt!!.text = "-" + data.total
                total_txt.setTextColor(MConst.expendTextColor)
            }

            mainLayout!!.setOnClickListener{
                mContext.startActivity<BillActivity>(
                        MConst.CURRENT_MOUTH_WITH_YEAR to Calendar.getInstance().getCurrenMouthWidthYear()
                )
             }
        }
    }
}